﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class LekachimCard : UserControl
    {
        string TableName = "Lekachim";
        IMofa mofa;
        DataTable dt = new DataTable();
        SqlDataAdapter da;

        public LekachimCard(IMofa mofa)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            this.mofa = mofa;

            string selectStr = "SELECT * FROM [Table] WHERE startDate=@startDate";
            string updateStr = CompanyManager.getSqlStringFromFile(@"\UPDATE\Lekachim.UPDATE.sql");
            string deleteStr = "DELETE FROM [Table] WHERE lekachID=@lekachID";
            string insertStr = "INSERT INTO [Table] VALUES (@startDate,@mofaType, @lekachType, @lekachData)";

            selectStr = selectStr.Replace("Table", TableName);
            updateStr = updateStr.Replace("Table", TableName);
            deleteStr = deleteStr.Replace("Table", TableName);
            insertStr = insertStr.Replace("Table", TableName);

            da = new SqlDataAdapter(selectStr, CompanyManager.Connection);
            da.UpdateCommand = new SqlCommand(updateStr, da.SelectCommand.Connection);
            da.DeleteCommand = new SqlCommand(deleteStr, da.SelectCommand.Connection);
            da.InsertCommand = new SqlCommand(insertStr, da.SelectCommand.Connection);

            ICollection<string> sqlParameterCollection = CompanyManager.parseSqlQueryParameters(updateStr);
            sqlParameterCollection.Distinct<string>();

            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
            {
                string sItem = "@" + item;
                switch (item)
                {
                    case "lekachID":
                        da.UpdateCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.Int, DataRowVersion.Original, item));
                        da.DeleteCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.Int, DataRowVersion.Original, item));
                        break;
                    case "startDate":
                        da.SelectCommand.Parameters.Add("@startDate",SqlDbType.Date).Value = mofa.StartDate;
                        da.UpdateCommand.Parameters.Add("@startDate", SqlDbType.Date).Value = mofa.StartDate;
                        da.InsertCommand.Parameters.Add("@startDate", SqlDbType.Date).Value = mofa.StartDate;
                        break;
                    case "mofaType":
                        da.UpdateCommand.Parameters.Add(sItem, SqlDbType.NVarChar).Value = mofa.ToString();
                        da.InsertCommand.Parameters.Add(sItem, SqlDbType.NVarChar).Value = mofa.ToString();
                        break;
                    case "lekachType":
                        da.UpdateCommand.Parameters.Add(sItem, SqlDbType.NVarChar).SourceColumn = "lekachType";
                        da.InsertCommand.Parameters.Add(sItem, SqlDbType.NVarChar).SourceColumn = "lekachType";
                        break;
                    case "lekachData":
                        da.UpdateCommand.Parameters.Add(sItem, SqlDbType.NVarChar).SourceColumn = "lekachData";
                        da.InsertCommand.Parameters.Add(sItem, SqlDbType.NVarChar).SourceColumn = "lekachData";
                        break;
                }
            }
            da.Fill(dt);
            dataGridView.DataSource = dt;

            dataGridView.Columns["ColumnLekachType"].DataPropertyName = "lekachType";
            dataGridView.Columns["ColumnLekachData"].DataPropertyName = "lekachData";

            dataGridView.Columns["lekachID"].Visible = false;
            dataGridView.Columns["startDate"].Visible = false;
            dataGridView.Columns["mofaType"].Visible = false;
            dataGridView.Columns["lekachType"].Visible = false;
            dataGridView.Columns["lekachData"].Visible = false;
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        { labelSuccessfulUpdate.Visible = false; }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            da.Update(dt);
            dt.Clear();
            da.Fill(dt);

            labelSuccessfulUpdate.Visible = true;
            labelSuccessfulUpdate.Location = new Point(splitContainer.Panel1.Bounds.Width / 2, 11);
            timer1.Interval = 5 * 1000;
            timer1.Start();
        }

        public override string ToString()
        {  return "לקחים"; }

        private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
      
    }
}
